//
//  AppUtils.m
//  ProDemo
//
//  Created by ylh on 13-12-25.
//  Copyright (c) 2013年 ylh. All rights reserved.
//

#import "ValidateUtils.h"

@implementation ValidateUtils

+ (BOOL)isEmail:(NSString *)checkString
{
    BOOL stricterFilter = YES; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}

+(BOOL) isMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^(1)\\d{10}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    //    NSLog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:mobile];
}

+(BOOL) isNumber:(NSString *)money
{
    NSString *phoneRegex = @"^\\d{1,}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:money];
}


+(BOOL) isMoney:(NSString *)money
{
    return [self isMoney:money maxDecimalsCount:-1];
}

+(BOOL) is2Money:(NSString *)money
{
    return [self isMoney:money maxDecimalsCount:2];
}

+(BOOL) isMoney:(NSString *)money maxDecimalsCount:(NSInteger)count
{
    NSString *phoneRegex = @"^(\\d+)+$";
    if (count>0) {
         phoneRegex = [NSString stringWithFormat:@"^(\\d+)+(\\.\\d{0,%d})?$",count];
    }else if(count<0){
        phoneRegex = @"^(\\d+)+(\\.\\d{1,})?$";
    }
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    NSPredicate *phoneTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^0(\\d+)$"];
    return [phoneTest evaluateWithObject:money] && ![phoneTest1 evaluateWithObject:money];
}



+(BOOL) isBankCard:(NSString *)card{
    NSString *phoneRegex = @"^\\d{16,19}$|^\\d{6}[- ]\\d{10,13}$|^\\d{4}[- ]\\d{4}[- ]\\d{4}[- ]\\d{4,7}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:card];
}

+(BOOL) isPersonCard:(NSString *)card{
    NSString *phoneRegex = @"^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X|x)$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:card];
}


@end
